/**
 * 转换器
 * @author 赵振华
 * 主要的功能就是负责，将html代码快速的转化为jDreamer对象。
 * 这项功能主要提供给html开发者，让html开发者尽量少的书写js代码
 */
(function( _ , $ ){
	var handlerMap = new _.Map();
	var disableHandlerMap = new _.Map();
	/**
	 * 处理器核心函数
	 * @param _region	处理的区域
	 * @param _parent	父级jDreamer对象
	 * @param _changerName	一个数组，可以指定转化器执行特定的转化器
	 */
	var Changer = function( _region , _changerName){
		try{
			var region = $(_region);
			if(_changerName == undefined){
				handlerMap.call(function(obj){
					var selector = obj.selector;
					var handler = obj.handler;
					var objs = region.find(selector);
					if(objs.size() > 0){
						handler( objs ,_region);
					}
				});
			}else{
				for(i in _changerName){
					var name = _changerName[i];
					var obj = handlerMap.get(name);
					if(obj){
						var selector = obj.selector;
						var handler = obj.handler;
						var objs = region.find(selector);
						if(objs.size() > 0){
							handler( objs , _region );
						}
					}
				}
			}
		}catch(e){
			alert(e);
		}
	};
	Changer.handlerMap = handlerMap;
	Changer.disableHandlerMap = disableHandlerMap;
	/**
	 * 添加处理器
	 * @param _name	处理器名称
	 * @param _fn	具体处理函数
	 */
	Changer.addHandler = function(_handler){
		var name = _handler.name;
		var h = handlerMap.get(name);
		if(h != undefined){
			//_.Logger("jDreamer.Changer.addHandler() " + _.LangPack.changer_add_handler,[name]);
		}
		handlerMap.put(name , _handler);
	};
	
	Changer.disable = function(_name){
		var h = handlerMap.get(_name);
		if(h == undefined){
			//_.Logger("jDreamer.Changer.disable() " + _.LangPack.changer_disable,[_name]);
		}else{
			disableHandlerMap.put(_name,h);
			handlerMap.remove(_name);
		}
	};
	Changer.enable = function(_name){
		var h = disableHandlerMap.get(_name);
		if(h == undefined){
			//_.Logger("jDreamer.Changer.enable() "+ _.LangPack.changer_enable,[ _name , _name ]);
		}else{
			handlerMap.put(_name,h);
			disableHandlerMap.remove(_name);
		}
	};
	_.Changer = Changer;
})(jDreamer,jQuery);